/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { Result, Button } from 'antd';
import React, { useEffect, useState } from 'react';
import type { Location } from 'umi';
import { useLocation } from 'umi';

const AuthPhone: React.FC<{}> = () => {
  const location = useLocation() as Location;
  const [title, setTitle] = useState<string>('请前往企业微信app个人信息中绑定手机号后即可登录');
  const loginFailure = new URLSearchParams(location.search).get('longTime');
  const redirect_uri = new URLSearchParams(location.search).get('redirect');
  const type = new URLSearchParams(location.search).get('type') || '';

  const tips = {
    new: `请联系管理员<a>束霞18505650937</a>获取组织架构权限`,
  };

  const getMenuAuth = () => {
    const ruri = `https://admin.kuaidihelp.com/Login/auth?redirect_uri=${redirect_uri}`;
    window.open(
      `https://open.weixin.qq.com/connect/oauth2/authorize?appid=ww43db307c16112331&redirect_uri=${encodeURIComponent(
        ruri,
      )}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`,
      '_self',
    );
  };
  useEffect(() => {
    if (loginFailure) {
      getMenuAuth();
      setTitle('您长时间未进行操作系统认为您已离线，需重新登陆');
    } else if (tips[type]) {
      setTitle(tips[type]);
    }
  }, [location.search]);

  return (
    <Result
      title={<div dangerouslySetInnerHTML={{ __html: title }}></div>}
      extra={
        loginFailure || tips[type] ? (
          <Button type='primary' onClick={getMenuAuth}>
            重新登录
          </Button>
        ) : null
      }
    />
  );
};

export default AuthPhone;
